var express = require('express'),
    bodyParser = require('body-parser'),
    path = require('path'),
    config=require('./config/config'),
    users=require('./routers/users'),
    products=require('./routers/product'),
    session=require('express-session'),
    MongoStore=require('connect-mongo')(session);
var app = express();
app.use(session({
    secret: 'user', // 用来注册session id 到cookie中，相当与一个密钥
    name: 'user', // cookie的name
    cookie: {maxAge: 2592000000},
    resave: false,
    saveUninitialized: true,
    store: new MongoStore({
        url: 'mongodb://'+config.dbhost+':'+config.dbport+'/'+config.dbname,
        autoRemove: 'native'
    })
}));
app.use(express.static(path.join(__dirname, 'src')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(users);
app.use(products);
app.use(function (req,res) {
    res.status(404).json({msg:'404 not Found'});
});
app.listen(config.port,config.host,function (err) {
    if (err)
        throw err;
    console.log('server starts at port 3000');
});